package com.ccl.iot.device;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.ccl.iot.IOTInterface.IOTStateMonitorInterface;
import com.ccl.iot.IOTInterface.IOTStateMonitorInterface.IOTStateChangeListener;
import com.ccl.iot.R;
import com.ccl.iot.dvr.utils.CommonUtils;
import com.ccl.iot.dvr.utils.GlideUtil;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.dvr.utils.LoggerUtil;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.views.WaitRegView;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 空调的view
 * @author： HD
 * @time：2017/3/20
 */
public class AirControllerView extends LinearLayout  {

    private Context mContext;
    private FrameLayout mContainer;

    private IOTAirCondition mDev;

    public ImageView mDeviceImg;

    private SeekBar mAirSeekBar;
    private ImageView mAirModeImg;
    private TextView mAirModeName;
    private ImageView mAirSpeedImg;
    private TextView mAirSpeedName;
    private ImageView mAirDirImg;
    private TextView mAirDirName;

    private ImageView mAirTemp16Img, mAirTemp17Img, mAirTemp18Img, mAirTemp19Img, mAirTemp20Img, mAirTemp21Img,
            mAirTemp22Img, mAirTemp23Img, mAirTemp24Img, mAirTemp25Img, mAirTemp26Img, mAirTemp27Img, mAirTemp28Img,
            mAirTemp29Img, mAirTemp30Img, mAirTemp31Img, mAirTemp32Img;

    private ImageView mAirSWitchImg;
    private View mAirModePopView;
    private PopupWindow mAirModePop;
    private LinearLayout mAirModeLayout;
    private LinearLayout mAirSpeedLayout;
    private LinearLayout mAirDirLayout;
    private LinearLayout mAirBtnListLayout;
    private int mScreenWidth;
    /**
     * PopupWindow的宽度
     */
    private int mPopViewWidth;
    private PopupWindow mAirSpeedPop;
    private View mAirSpeedPopView;
    private View mAirDirPopView;
    private PopupWindow mAirDirPop;
    private LinearLayout mAirModeWetLayout;
    private LinearLayout mAirModeWindLayout;
    private LinearLayout mAirModeHotLayout;
    private LinearLayout mAirModeCoolLayout;
    private LinearLayout mAirModeAutoLayout;
    private LinearLayout mAirSpeedShowLayout;
    private LinearLayout mAirSpeedMiddleLayout;
    private LinearLayout mAirSpeedFastLayout;
    private LinearLayout mAirDir0Layout;
    private LinearLayout mAirDir15Layout;
    private LinearLayout mAirDir30Layout;
    private LinearLayout mAirDir45Layout;
    private LinearLayout mAirDir90Layout;
    private LinearLayout mAirDirAutoLayout;


    private LinearLayout mAirSwitchLayout;
    private WaitRegView mWaitRegView;
    private List<ImageView> mTempImgList;

    public LinearLayout mCameraLayout;
    private FrameLayout mAirLayout;

    public AirControllerView(Context context, IOTAirCondition dev) {
        super(context);
        mDev = dev;
        mContext = context;
        mDev.addStateChangeListener(mStateListener);
        initView();
        initData();
        initListener();

//        getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//            }
//        });
    }

    private void initView() {
        View.inflate(mContext, R.layout.pop_air_controller, this);
        mContainer = (FrameLayout) findViewById(R.id.layout_container);
        mAirLayout = (FrameLayout) findViewById(R.id.layout_air);

        mDeviceImg = (ImageView) findViewById(R.id.iv_air);
        mWaitRegView = (WaitRegView) findViewById(R.id.waitregview_air);
        mCameraLayout = (LinearLayout) findViewById(R.id.layout_camera_frame);

        mAirTemp16Img = (ImageView) findViewById(R.id.iv_air_16);
        mAirTemp17Img = (ImageView) findViewById(R.id.iv_air_17);
        mAirTemp18Img = (ImageView) findViewById(R.id.iv_air_18);
        mAirTemp19Img = (ImageView) findViewById(R.id.iv_air_19);
        mAirTemp20Img = (ImageView) findViewById(R.id.iv_air_20);
        mAirTemp21Img = (ImageView) findViewById(R.id.iv_air_21);
        mAirTemp22Img = (ImageView) findViewById(R.id.iv_air_22);
        mAirTemp23Img = (ImageView) findViewById(R.id.iv_air_23);
        mAirTemp24Img = (ImageView) findViewById(R.id.iv_air_24);
        mAirTemp25Img = (ImageView) findViewById(R.id.iv_air_25);
        mAirTemp26Img = (ImageView) findViewById(R.id.iv_air_26);
        mAirTemp27Img = (ImageView) findViewById(R.id.iv_air_27);
        mAirTemp28Img = (ImageView) findViewById(R.id.iv_air_28);

        mAirTemp29Img = (ImageView) findViewById(R.id.iv_air_29);
        mAirTemp30Img = (ImageView) findViewById(R.id.iv_air_30);
        mAirTemp31Img = (ImageView) findViewById(R.id.iv_air_31);
        mAirTemp32Img = (ImageView) findViewById(R.id.iv_air_32);

        mTempImgList = new ArrayList<ImageView>();
        mTempImgList.add(mAirTemp16Img);
        mTempImgList.add(mAirTemp17Img);
        mTempImgList.add(mAirTemp18Img);
        mTempImgList.add(mAirTemp19Img);
        mTempImgList.add(mAirTemp20Img);
        mTempImgList.add(mAirTemp21Img);
        mTempImgList.add(mAirTemp22Img);
        mTempImgList.add(mAirTemp23Img);
        mTempImgList.add(mAirTemp24Img);
        mTempImgList.add(mAirTemp25Img);
        mTempImgList.add(mAirTemp26Img);
        mTempImgList.add(mAirTemp27Img);
        mTempImgList.add(mAirTemp28Img);
        mTempImgList.add(mAirTemp29Img);
        mTempImgList.add(mAirTemp30Img);
        mTempImgList.add(mAirTemp31Img);
        mTempImgList.add(mAirTemp32Img);


        mAirSeekBar = (SeekBar) findViewById(R.id.seekBar_air);
        mAirSeekBar.setMax(160);

        mAirModeLayout = (LinearLayout) findViewById(R.id.layout_airmode);
        mAirSpeedLayout = (LinearLayout) findViewById(R.id.layout_airspeed);
        mAirDirLayout = (LinearLayout) findViewById(R.id.layout_airdir);
        mAirSwitchLayout = (LinearLayout) findViewById(R.id.layout_air_switch);

        mAirBtnListLayout = (LinearLayout) findViewById(R.id.layout_airbtn_list);

        mAirModeImg = (ImageView) findViewById(R.id.iv_airmode);
        mAirModeName = (TextView) findViewById(R.id.tv_airmode_name);

        mAirSpeedImg = (ImageView) findViewById(R.id.iv_airspeed);
        mAirSpeedName = (TextView) findViewById(R.id.tv_airspeed_name);

        mAirDirImg = (ImageView) findViewById(R.id.iv_airdir);
        mAirDirName = (TextView) findViewById(R.id.tv_airdir_name);

        mAirSWitchImg = (ImageView) findViewById(R.id.iv_air_switch);

    }

    private void initListener() {

        mAirTemp16Img.setOnClickListener(mTempListener);
        mAirTemp17Img.setOnClickListener(mTempListener);
        mAirTemp18Img.setOnClickListener(mTempListener);
        mAirTemp19Img.setOnClickListener(mTempListener);
        mAirTemp20Img.setOnClickListener(mTempListener);
        mAirTemp21Img.setOnClickListener(mTempListener);
        mAirTemp22Img.setOnClickListener(mTempListener);
        mAirTemp23Img.setOnClickListener(mTempListener);
        mAirTemp24Img.setOnClickListener(mTempListener);
        mAirTemp25Img.setOnClickListener(mTempListener);
        mAirTemp26Img.setOnClickListener(mTempListener);
        mAirTemp27Img.setOnClickListener(mTempListener);
        mAirTemp28Img.setOnClickListener(mTempListener);

        mAirTemp29Img.setOnClickListener(mTempListener);
        mAirTemp30Img.setOnClickListener(mTempListener);
        mAirTemp31Img.setOnClickListener(mTempListener);
        mAirTemp32Img.setOnClickListener(mTempListener);
        //------------------------
        //        mAirModeImg.setOnClickListener(mListener);
        //        mAirSpeedImg.setOnClickListener(mListener);
        //        mAirDirImg.setOnClickListener(mListener);

        mAirModeLayout.setOnClickListener(mListener);
        mAirSpeedLayout.setOnClickListener(mListener);
        mAirDirLayout.setOnClickListener(mListener);
        mAirSwitchLayout.setOnClickListener(mListener);


        mAirSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //停的时候才改变温度
                int temp = seekbarValue2Temp(seekBar.getProgress());
                                LogUtil.i("温度：" + temp);
                if (mDev != null) {
                    mDev.setTemp(temp);
                }
            }
        });
    }

    private IOTStateChangeListener mStateListener = new IOTStateChangeListener() {

        @Override
        public void onStateChanged(IOTStateMonitorInterface aSource, int aNewState) {
            UIUtils.postTaskSafely(new Runnable() {
                @Override
                public void run() {
                    updateUI();
                }
            });
        }
    };

    private void initData() {
        DisplayMetrics metrics = CommonUtils.getMetrics(mContext);
        mScreenWidth = metrics.widthPixels;
        mPopViewWidth = (int) ((float) mScreenWidth / 4);
    }

    /**
     * 设置成对话框的界面
     */
    public void setDialogLayout() {
        mAirLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPopViewWidth = (int) ((float) mAirLayout.getWidth() / 4);
            }
        }, 800);

    }

    private View.OnClickListener mTempListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int seebBarProgress = 0;
            switch (v.getId()) {
                case R.id.iv_air_16:
                    seebBarProgress = 0;
                    break;
                case R.id.iv_air_17:
                    seebBarProgress = 10;
                    break;
                case R.id.iv_air_18:
                    seebBarProgress = 20;
                    break;
                case R.id.iv_air_19:
                    seebBarProgress = 30;
                    break;
                case R.id.iv_air_20:
                    seebBarProgress = 40;
                    break;
                case R.id.iv_air_21:
                    seebBarProgress = 50;
                    break;
                case R.id.iv_air_22:
                    seebBarProgress = 60;
                    break;
                case R.id.iv_air_23:
                    seebBarProgress = 70;
                    break;
                case R.id.iv_air_24:
                    seebBarProgress = 80;
                    break;
                case R.id.iv_air_25:
                    seebBarProgress = 90;
                    break;
                case R.id.iv_air_26:
                    seebBarProgress = 100;
                    break;
                case R.id.iv_air_27:
                    seebBarProgress = 110;
                    break;
                case R.id.iv_air_28:
                    seebBarProgress = 120;
                    break;
                case R.id.iv_air_29:
                    seebBarProgress = 130;
                    break;
                case R.id.iv_air_30:
                    seebBarProgress = 140;
                    break;
                case R.id.iv_air_31:
                    seebBarProgress = 150;
                    break;
                case R.id.iv_air_32:
                    seebBarProgress = 160;
                    break;
                default:
                    break;
            }

            mAirSeekBar.setProgress(seebBarProgress);

            int temp = seekbarValue2Temp(seebBarProgress);
            if (mDev != null) {
                mDev.setTemp(temp);
            }

            CommonUtils.showWaitRegViewOnMainThread(mWaitRegView);
        }
    };


    private View.OnClickListener mModeListener = new OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mDev != null) {

                switch (v.getId()) {

                    //模式pop弹出来的5个按钮
                    case R.id.layout_airmode_wet:

                        mDev.setMode(5);
                        break;
                    case R.id.layout_airmode_wind:

                        mDev.setMode(2);
                        break;
                    case R.id.layout_airmode_hot:

                        mDev.setMode(4);
                        break;
                    case R.id.layout_airmode_cool:

                        mDev.setMode(3);
                        break;
                    case R.id.layout_airmode_auto:

                        mDev.setMode(1);
                        break;
                    default:
                        break;
                }

                mAirModePop.dismiss();

                CommonUtils.showWaitRegViewOnMainThread(mWaitRegView);
            }

        }
    };

    /**
     * 更新模式图片和名字
     *
     * @param mode
     * @return
     */
    private void updateAirModeImgAndName(byte mode) {
        int imgId = R.drawable.icon_airmode_auto;
        int txtId = R.string.auto;
        switch (mode) {
            case 1:
                imgId = R.drawable.icon_airmode_auto;
                txtId = R.string.auto;
                break;
            case 2:
                imgId = R.drawable.icon_airmode_wind;
                txtId = R.string.airmode_wind;
                break;
            case 3:
                imgId = R.drawable.icon_airmode_cool;
                txtId = R.string.airmode_cool;
                break;
            case 4:
                imgId = R.drawable.icon_airmode_hot;
                txtId = R.string.airmode_hot;
                break;
            case 5:
                imgId = R.drawable.icon_airmode_wet;
                txtId = R.string.airmode_wet;
                break;
            default:
                imgId = R.drawable.icon_unknown;
                txtId = R.string.Unknown;
                break;
        }
//        mAirModeImg.setImageResource(imgId);
        GlideUtil.loadImageView(mContext,imgId,mAirModeImg);
        mAirModeName.setText(getResources().getString(txtId));
    }


    private View.OnClickListener mSpeedListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mDev != null) {

                switch (v.getId()) {

                    //风速pop弹出来的3个按钮
                    case R.id.layout_airspeed_slow:

                        mDev.setSpeed(1);
                        break;
                    case R.id.layout_airspeed_middle:

                        mDev.setSpeed(2);
                        break;
                    case R.id.layout_airspeed_fast:

                        mDev.setSpeed(3);
                        break;
                    default:
                        break;
                }

                mAirSpeedPop.dismiss();

                CommonUtils.showWaitRegViewOnMainThread(mWaitRegView);
            }
        }
    };

    /**
     * 更新风速图片和名字
     *
     * @param speed
     */
    private void updateAirSpeedImgAndName(byte speed) {
        int imgId = R.drawable.icon_airspeed_slow;
        int txtId = R.string.airspeed_slow;
        switch (speed) {

            //风速pop弹出来的3个按钮
            case 1:
                imgId = R.drawable.icon_airspeed_slow;
                txtId = R.string.airspeed_slow;
                break;
            case 2:
                imgId = R.drawable.icon_airspeed_middle;
                txtId = R.string.airspeed_middle;
                break;
            case 3:
                imgId = R.drawable.icon_airspeed_fast;
                txtId = R.string.airspeed_fast;
                break;
            default:
                break;
        }
//        mAirSpeedImg.setImageResource(imgId);
        GlideUtil.loadImageView(mContext,imgId,mAirSpeedImg);
        mAirSpeedName.setText(getResources().getString(txtId));
    }


    /**
     * 扫风的pop点击事件
     */
    private View.OnClickListener mDirListener = new OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mDev != null) {

                switch (v.getId()) {

                    //扫风pop弹出来的6个按钮
                    case R.id.layout_airdir_0:

                        mDev.setDirection(2);
                        break;
                    case R.id.layout_airdir_15:

                        mDev.setDirection(3);
                        break;
                    case R.id.layout_airdir_30:

                        mDev.setDirection(4);
                        break;
                    case R.id.layout_airdir_45:

                        mDev.setDirection(5);
                        break;
                    case R.id.layout_airdir_90:

                        mDev.setDirection(6);
                        break;
                    case R.id.layout_airdir_auto:

                        mDev.setDirection(1);
                        break;
                    default:
                        break;
                }

                mAirDirPop.dismiss();

                CommonUtils.showWaitRegViewOnMainThread(mWaitRegView);
            }
        }
    };

    /**
     * 更新扫风图片和名字
     *
     * @param dir
     */
    private void updateAirDirImgAndName(byte dir) {
        int imgId = R.drawable.icon_airdir_0;
        int txtId = R.string.airdir_0;
        switch (dir) {

            //扫风pop弹出来的6个按钮
            case 2:
                imgId = R.drawable.icon_airdir_0;
                txtId = R.string.airdir_0;
                break;
            case 3:
                imgId = R.drawable.icon_airdir_15;
                txtId = R.string.airdir_15;
                break;
            case 4:
                imgId = R.drawable.icon_airdir_30;
                txtId = R.string.airdir_30;
                break;
            case 5:
                imgId = R.drawable.icon_airdir_45;
                txtId = R.string.airdir_45;
                break;
            case 6:
                imgId = R.drawable.icon_airdir_90;
                txtId = R.string.airdir_90;
                break;
            case 1:
                imgId = R.drawable.icon_airdir_auto;
                txtId = R.string.auto;
                break;
            default:
                break;
        }
//        mAirDirImg.setImageResource(imgId);
        GlideUtil.loadImageView(mContext,imgId,mAirDirImg);
        mAirDirName.setText(getResources().getString(txtId));
    }

    private View.OnClickListener mListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mDev != null) {

                LoggerUtil.i("选中的ID：" + v.getId());
                switch (v.getId()) {

                    //底部4个按钮的点击事件
                    case R.id.layout_airmode:
                        showAirModePop();
                        break;
                    case R.id.layout_airspeed:
                        showAirSpeedPop();
                        break;
                    case R.id.layout_airdir:
                        showAirDirPop();
                        break;
                    case R.id.layout_air_switch:
                        //空调开关
                        boolean command = !mDev.isOn();
                        mDev.turn(command);

                        CommonUtils.showWaitRegViewOnMainThread(mWaitRegView);
                        break;

                    default:
                        break;
                }
            }
        }
    };


    /**
     * 将进度值转成温度
     *
     * @param progress
     */
    private int seekbarValue2Temp(int progress) {
        return (int)(Math.round((double)progress / 10d) + 16d);
    }

    /**
     * 温度值转seekbar进度值
     *
     * @param temp
     * @return
     */
    private int temp2SeekbarValue(int temp) {
        if (temp >= 16 && temp <= 32) {
            return (temp - 16) * 10;
        }
        return 0;
    }

    /**
     * 弹出模式PopupWindow
     */
    private void showAirModePop() {
        if (mAirModePopView == null) {
            mAirModePopView = inflate(mContext, R.layout.pop_air_mode, null);
            mAirModeWetLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_wet);
            mAirModeWindLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_wind);
            mAirModeHotLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_hot);
            mAirModeCoolLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_cool);
            mAirModeAutoLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_auto);

            mAirModeWetLayout.setOnClickListener(mModeListener);
            mAirModeWindLayout.setOnClickListener(mModeListener);
            mAirModeHotLayout.setOnClickListener(mModeListener);
            mAirModeCoolLayout.setOnClickListener(mModeListener);
            mAirModeAutoLayout.setOnClickListener(mModeListener);
        }

        if (mAirModePop == null) {
            mAirModePop = new PopupWindow(mAirModePopView, mPopViewWidth, 704);
            // 设置动画
            //            mAirModePop.setAnimationStyle(R.style.take_photo_anim);
            //  设置背景颜色
            mAirModePop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            // 设置可以获取焦点
            mAirModePop.setFocusable(true);
            // 设置可以触摸弹出框以外的区域
            mAirModePop.setOutsideTouchable(true);

        }

        if (mAirModePop.isShowing()) {
            mAirModePop.dismiss();
        } else {
            //  以下拉的方式显示，并且可以设置显示的位置
            mAirModePop.showAsDropDown(mAirBtnListLayout, 0, 0);
        }

    }

    /**
     * 弹出风速PopupWindow
     */
    private void showAirSpeedPop() {
        if (mAirSpeedPopView == null) {
            mAirSpeedPopView = inflate(mContext, R.layout.pop_air_speed, null);

            mAirSpeedShowLayout = (LinearLayout) mAirSpeedPopView.findViewById(R.id.layout_airspeed_slow);
            mAirSpeedMiddleLayout = (LinearLayout) mAirSpeedPopView.findViewById(R.id.layout_airspeed_middle);
            mAirSpeedFastLayout = (LinearLayout) mAirSpeedPopView.findViewById(R.id.layout_airspeed_fast);

            mAirSpeedShowLayout.setOnClickListener(mSpeedListener);
            mAirSpeedMiddleLayout.setOnClickListener(mSpeedListener);
            mAirSpeedFastLayout.setOnClickListener(mSpeedListener);

        }

        if (mAirSpeedPop == null) {

            mAirSpeedPop = new PopupWindow(mAirSpeedPopView, mPopViewWidth, 424);
            // 设置动画
            //            mAirSpeedPop.setAnimationStyle(R.style.take_photo_anim);
            //  设置背景颜色
            mAirSpeedPop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            // 设置可以获取焦点
            mAirSpeedPop.setFocusable(true);
            // 设置可以触摸弹出框以外的区域
            mAirSpeedPop.setOutsideTouchable(true);


        }

        if (mAirSpeedPop.isShowing()) {
            mAirSpeedPop.dismiss();
        } else {
            // 以下拉的方式显示，并且可以设置显示的位置
            mAirSpeedPop.showAsDropDown(mAirBtnListLayout, mPopViewWidth, 0);
        }

    }

    /**
     * 弹出扫风PopupWindow
     */
    private void showAirDirPop() {
        if (mAirDirPopView == null) {
            mAirDirPopView = inflate(mContext, R.layout.pop_air_dir, null);

            mAirDir0Layout = (LinearLayout) mAirDirPopView.findViewById(R.id.layout_airdir_0);
            mAirDir15Layout = (LinearLayout) mAirDirPopView.findViewById(R.id.layout_airdir_15);
            mAirDir30Layout = (LinearLayout) mAirDirPopView.findViewById(R.id.layout_airdir_30);
            mAirDir45Layout = (LinearLayout) mAirDirPopView.findViewById(R.id.layout_airdir_45);
            mAirDir90Layout = (LinearLayout) mAirDirPopView.findViewById(R.id.layout_airdir_90);
            mAirDirAutoLayout = (LinearLayout) mAirDirPopView.findViewById(R.id.layout_airdir_auto);

            mAirDir0Layout.setOnClickListener(mDirListener);
            mAirDir15Layout.setOnClickListener(mDirListener);
            mAirDir30Layout.setOnClickListener(mDirListener);
            mAirDir45Layout.setOnClickListener(mDirListener);
            mAirDir90Layout.setOnClickListener(mDirListener);
            mAirDirAutoLayout.setOnClickListener(mDirListener);
        }

        if (mAirDirPop == null) {

            mAirDirPop = new PopupWindow(mAirDirPopView, mPopViewWidth, 845);
            // 设置动画
            //            mAirDirPop.setAnimationStyle(R.style.take_photo_anim);
            //  设置背景颜色
            mAirDirPop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            // 设置可以获取焦点
            mAirDirPop.setFocusable(true);
            // 设置可以触摸弹出框以外的区域
            mAirDirPop.setOutsideTouchable(true);

        }

        if (mAirDirPop.isShowing()) {
            mAirDirPop.dismiss();
        } else {
            // 以下拉的方式显示，并且可以设置显示的位置
            mAirDirPop.showAsDropDown(mAirBtnListLayout, mPopViewWidth + mPopViewWidth, 0);
        }

    }

    public void removeListener() {
        mDev.removeStateChangeListener(mStateListener);
    }

    public void setDevice(IOTAirCondition dev) {
        if (mDev != null) {
            mDev.removeStateChangeListener(mStateListener);
        }

        mDev = dev;
        mDev.addStateChangeListener(mStateListener);

        updateUI();
    }


    /**
     * 根据数据刷新UI
     */
    public void updateUI() {
        LoggerUtil.i("更新UI");
        updateAirModeImgAndName((byte) mDev.getMode());
        updateAirSpeedImgAndName((byte) mDev.getSpeed());
        updateAirDirImgAndName((byte) mDev.getDirection());
        mAirSWitchImg.setSelected(mDev.isOn());

        //更新空调的主图片
        if (mDev.getImageCount() == 0) {
            int imageId = mDev.getImageId();
//            mDeviceImg.setImageResource(imageId);
            mDeviceImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            GlideUtil.loadImageView(mContext,imageId,mDeviceImg);

        } else {
//            Bitmap image = mDev.getImage();
//            mDeviceImg.setImageBitmap(image);
            mDeviceImg.setScaleType(ImageView.ScaleType.CENTER);
            String imagePath = mDev.getImagePath();
            GlideUtil.loadImageViewNoCacheDisk(mContext,imagePath,mDeviceImg);
        }

        CommonUtils.hideWaitRegViewOnMainThread(mWaitRegView);

        //更新设置温度
        byte setTemp = (byte) mDev.getTemp();
        mAirSeekBar.setProgress(temp2SeekbarValue(setTemp));

        //更新实时温度图片
        //            for (ImageView imageView : mTempImgList) {
        //                imageView.setSelected(false);
        //            }
        //            byte realTemp = mData[IOTAirCondition.REAL_TEMP];
        ////            LoggerUtil.i("实际温度：" + realTemp);
        //
        //            if (realTemp < 16){
        //                realTemp = 16;
        //            }
        //
        //            if (realTemp > 32) {
        //                realTemp = 32;
        //            }
        //
        //            int index = realTemp - 16 ;
        //
        //            mTempImgList.get(index).setSelected(true);

    }



}
